package com.wen.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.common.Result;
import com.wen.entity.Equipment;
import com.wen.service.EquipmentService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 设备器械管理
 *
 * @author dhh
 */
@RestController
@AllArgsConstructor
@RequestMapping("/equipment")
public class EquipmentController {

    private final EquipmentService equipmentService;

    //    添加设备信息
    @PostMapping
    public Result<?> save(@RequestBody Equipment equipment) {
        equipmentService.save(equipment);
        return Result.success();
    }

    //    更新
    @PutMapping
    public Result<?> update(@RequestBody Equipment equipment) {
        equipmentService.updateById(equipment);
        return Result.success();
    }

    //    s删除
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        equipmentService.removeById(id);
        return Result.success();
    }

    //    查询
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Equipment> wrapper = Wrappers.<Equipment>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Equipment::getEquipmentName, search);
        }
        Page<Equipment> NewsPage = equipmentService.page(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(NewsPage);
    }
}
